<?php

namespace app\common\model;

use app\api\service\R;
use think\Db;
use think\Model;
use think\model\concern\SoftDelete;
class StUser extends Model
{
    protected  $name = "st_user";
    protected $autoWriteTimestamp = true;// 自动写入时间戳

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;


    public function saveUserData($data){
        Db::startTrans();
        try {
            $arr = [];
            foreach ($data as $key=>$val){
                $arr[$key]= $val;
                $id = self::where(['user_code'=>trim($val['user_code']),'comid'=>$val['comid']])->value('id');
                if ($id) $arr[$key]['id']=$id;
            }

            $bool =  $this->saveAll($arr);
            return R::success();

        } catch (\Exception $e) {
            Db::rollback();
            return R::error($e->getMessage());
        }

    }


//    public static function addUserData($data){
//       return self::insertAll($data);
//    }
//
//    protected static function UpdateUserData($data)
//    {
//        return self::update($data);
//    }
}